package by.scsbsu.database.dao;

import by.scsbsu.database.tools.DbConnectionPool;

public class UserDAO extends AbstractDAO {
	public static final String SQL_USER_CREATE_USER = "INSERT INTO user ( id_bsu , name , surname , email , user_type , localization , id_student_group , password ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? );";
	public static final String SQL_USER_GET_ID_CREATE_USER = "SELECT id FROM user WHERE ( email = ? );";
	public static final String SQL_USER_DELETE_USER_BY_ID = "DELETE FROM user WHERE ( id = ? );";
	public static final String SQL_USER_GET_USER_BY_LOGIN_AND_PASSWORD = "SELECT * FROM user WHERE ( ( email = ? ) AND ( password = ? ) );";
	
	public boolean isUserValid(String login, String password){
		boolean result = false;
		if(login != null && password != null) {
			try {
				connection = DbConnectionPool.getConnection();
				
				statement = connection.prepareStatement(SQL_USER_GET_USER_BY_LOGIN_AND_PASSWORD);
				statement.setString(1, login);
				statement.setString(2, password);
				
				resultSet = statement.executeQuery();
				if(resultSet.next()){
					result = true;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				close();
			}
		}
		return result;
	}
}
